package com.songtech.server.utils;


import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * Create By YINN on 2018/4/6 22:45
 * Description : 格式转换工具类
 */
public class FormatDateUtils {

    private final static String DEFAULT_DATE_PATTERN = "ddMMyy";

    private final static String DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * Created by YINN on 2018/4/8 14:46.
     *
     * @param t HHMMSS字符串
     * @Description : 将报文中HHMMSS格式的time转化为localtime
     * @Result : LocalTime
     */
    public static LocalTime parseToLocalTime(String t) {

        if (StringUtils.isBlank(t)) {
            return null;
        }

        return LocalTime.of(Integer.valueOf(t.substring(0, 2)),
                Integer.valueOf(t.substring(2, 4)),
                Integer.valueOf(t.substring(4, 6)));
    }

    /**
     * Created by YINN on 2018/4/8 14:46.
     *
     * @param t DDMMYY字符串
     * @Description : 将报文中DDMMYY格式的日期转化为date
     * @Result : date
     */
    public static Date parseToDate(String t) throws ParseException {
        if (StringUtils.isBlank(t)) {
            return null;
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
        return dateFormat.parse(t);
    }


    /**
     * Created by YINN on 2018/4/8 14:46.
     *
     * @param t DDMMYY字符串
     * @Description : 将报文中DDMMYY格式的日期转化为date
     * @Result : date
     */
    public static Date parse(String t) throws ParseException {
        if (StringUtils.isBlank(t)) {
            return null;
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN);
        return dateFormat.parse(t);
    }

    /**
     * Created by YINN on 2018/4/27 10:59.
     *
     * @param localDateTime
     * @Description : LocalDateTime --> Date
     * @Result :
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {

        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return Date.from(instant);
    }

    /**
     * Created by YINN on 2018/4/27 10:59.
     *
     * @param localTime
     * @Description : localTime --> Date
     * @Result :
     */
    public static Date LocalTimeParseToDate(LocalTime localTime) {
        LocalDate localDate = LocalDate.now();
        LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return Date.from(instant);
    }

    /**
     * Created by YINN on 2018/4/27 10:59.
     *
     * @param start
     * @param end
     * @Description : 计算分钟差
     * @Result :
     */
    public static int getMinite(long start, long end) {

        return getSeconds(start, end) / 60;

    }

    /**
     * Created by YINN on 2018/4/27 10:59.
     *
     * @param start
     * @param end
     * @Description : 计算秒差
     * @Result :
     */
    public static int getSeconds(long start, long end) {

        return Long.valueOf((end - start) / 1000).intValue();

    }

    public static int getSeconds(Date start, Date end) {

        return getSeconds(start.getTime(), end.getTime());

    }

    /**
     * Created by YINN on 2018/4/27 10:59.
     *
     * @param localDate
     * @param localTime
     * @Description : localDate + localTime -----> Date
     * @Result :
     */
    public static Date dateAndTimeToDate(LocalDate localDate, LocalTime localTime) {

        LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);

        return FormatDateUtils.localDateTimeToDate(localDateTime);
    }



}
